<!--
  * 用户列表
  *
  * @Author:    kaiyun
  * @Date:      2024-06-23 23:31:38
  * @Copyright  <a href="https://1024lab.net">1024创新实验室</a>
-->
<template>
  <!---------- 查询表单form begin ----------->  
  <a-form class="smart-query-form">
    <a-row class="smart-query-form-row">
      <a-form-item label="用户昵称" class="smart-query-form-item">
        <a-input style="width: 200px" v-model:value="queryForm.nickName" placeholder="请输入用户昵称" />
      </a-form-item>
      <a-form-item label="创建时间" class="smart-query-form-item">
        <a-range-picker v-model:value="createDateRange" :presets="defaultTimeRanges" style="width: 240px" @change="changeCreateDate" />
      </a-form-item>
      <a-form-item class="smart-query-form-item">
        <a-button-group>
          <a-button type="primary" @click="onSearch">
            <template #icon>
              <SearchOutlined />
            </template>
            查询
          </a-button>
        
        </a-button-group>
      </a-form-item>
    </a-row>
  </a-form>
  <!---------- 查询表单form end ----------->  

  <a-card size="small" :bordered="false" :hoverable="true">
    <!---------- 表格 begin ----------->  
    <a-table
      :columns="columns"
      :data-source="tableData"
      :loading="tableLoading"
      :pagination="{
        total: total,
        current: queryForm.pageNum,
        pageSize: queryForm.pageSize,
        showSizeChanger: true,
        showQuickJumper: true,
        showTotal: (total) => `共 ${total} 条`,
        onChange: changePage,
      }"
      :scroll="{ x: 800 }"
      row-key="userId"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'avatar'">
          <a-avatar :src="record.avatar" />
        </template>
      </template>
    </a-table>
    <!---------- 表格 end ----------->  
  </a-card>
</template>

<script setup>
  import { ref, reactive, onMounted } from 'vue';
  import { SearchOutlined, ReloadOutlined } from '@ant-design/icons-vue';
  import { defaultTimeRanges } from '/@/lib/default-time-ranges';
  import { message } from 'ant-design-vue';
  import { userApi } from '/@/api/business/bjd/user-api';



  // --------------------------- 表格列定义 ---------------------------
  const columns = ref([
    {
      title: '用户ID',
      dataIndex: 'id',
      width: 150,
      ellipsis: true,
    },
    {
      title: '用户昵称',
      dataIndex: 'nickName',
      width: 150,
      ellipsis: true,
    },
    {
      title: '头像',
      dataIndex: 'avatar',
      width: 80,
    },
    {
      title: 'OpenID',
      dataIndex: 'openId',
      width: 200,
      ellipsis: true,
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      width: 150,
    },
  ]);

  // --------------------------- 查询相关 ---------------------------
  const queryFormState = {
    nickName: undefined,
    createTimeBegin: undefined,
    createTimeEnd: undefined,
    pageNum: 1,
    pageSize: 10,
  };

  const queryForm = reactive({ ...queryFormState });
  const tableLoading = ref(false);
  const tableData = ref([]);
  const total = ref(0);
  const createDateRange = ref([]);


  // 查询数据
  function onSearch() {
    queryForm.pageNum = 1;
    queryData();
  }

  // 分页改变
  function changePage(page, pageSize) {
    queryForm.pageNum = page;
    queryForm.pageSize = pageSize;
    queryData();
  }

  // 创建时间改变
  function changeCreateDate(dates) {
    if (dates && dates.length === 2) {
      queryForm.createTimeBegin = dates[0];
      queryForm.createTimeEnd = dates[1];
    } else {
      queryForm.createTimeBegin = undefined;
      queryForm.createTimeEnd = undefined;
    }
  }

  // 查询数据方法
  async function queryData() {
    try {
      tableLoading.value = true;
      const result = await userApi.queryPage(queryForm);
      tableData.value = result.data.list;
      total.value = result.data.total;
    } catch (err) {
      console.error(err);
      message.error('查询失败');
    } finally {
      tableLoading.value = false;
    }
  }

  onMounted(() => {
    queryData();
  });
</script>